package org.ljx.taobao.controller;

import org.ljx.taobao.api.IUserService;
import org.ljx.taobao.api.entity.User;
import org.ljx.taobao.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Objects;

/**
 * @Author: Ljx
 * @Date: 2021/12/31 12:00
 * @role: 用户登录
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    static boolean x;
    public static String name1;
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;utf-8");
        HttpSession session = req.getSession();
        String name = req.getParameter("uname");
        String paw = req.getParameter("upaw");

        if (Objects.equals(name, "") || Objects.equals(paw, "")){
            req.setAttribute("msg","输入的值不能为空！！！");
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }else {
            session.setAttribute("uname",name);
            name1 = name;
            User user2 = new User(name,paw);
            IUserService iUserService = new UserServiceImpl();
            x = iUserService.login(user2);
            resp.sendRedirect("panduan.jsp");
        }
    }
    public static boolean rgs(){
        return x;
    }
}

